package com.example.log_db.controller;

import com.example.log_db.annotation.LogRecord;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 示例Controller
 */
@RestController
@RequestMapping("/api/demo")
public class DemoController {

    /**
     * 获取用户信息
     */
    @LogRecord(operationType = "查询", description = "获取用户信息")
    @GetMapping("/user/{id}")
    public Map<String, Object> getUser(@PathVariable Long id) {
        // 模拟查询用户
        Map<String, Object> user = new HashMap<>();
        user.put("id", id);
        user.put("name", "测试用户");
        user.put("age", 25);
        return user;
    }

    /**
     * 创建用户
     */
    @LogRecord(operationType = "新增", description = "创建用户")
    @PostMapping("/user")
    public Map<String, Object> createUser(@RequestBody UserDTO userDTO) {
        // 模拟创建用户
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "用户创建成功");
        result.put("data", userDTO);
        return result;
    }

    /**
     * 更新用户
     */
    @LogRecord(operationType = "更新", description = "更新用户信息")
    @PutMapping("/user/{id}")
    public Map<String, Object> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        // 模拟更新用户
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "用户更新成功");
        result.put("id", id);
        result.put("data", userDTO);
        return result;
    }

    /**
     * 删除用户
     */
    @LogRecord(operationType = "删除", description = "删除用户")
    @DeleteMapping("/user/{id}")
    public Map<String, Object> deleteUser(@PathVariable Long id) {
        // 模拟删除用户
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "用户删除成功");
        result.put("id", id);
        return result;
    }

    /**
     * 测试异常情况
     */
    @LogRecord(operationType = "异常测试", description = "测试异常日志记录")
    @GetMapping("/exception")
    public Map<String, Object> testException() {
        // 模拟异常
        throw new RuntimeException("测试异常");
    }

    /**
     * 用户DTO
     */
    @Data
    public static class UserDTO {
        private String name;
        private Integer age;
        private String email;
    }
}